% function BrookingsCrisisDecomp
% Check how the SigSplit code interprets the decompositions, given
% alternative decompositions of the signals between the factors and the
% idiosyncratic movements 

clear all; close all; 
cucd=pwd; 
loadFolder='O:\PROJ_LIB\LJM\exog\CEFJ\BigARMAHPSigSplit\Prior 9 Gap OverIdent HP FL\02 14 12';
loadFolderBase='O:\PROJ_LIB\LJM\exog\CEFJ\BigARMAHP\Prior9 Gap OverIdent HP FL\02 03 12'; 
saveFolder=cr_dir(loadFolder,'Big vs Base'); 
saveFolderAlt=cr_dir(loadFolder,'Big Alt'); 
report.compare.big={'FFR Lead 1','FFR Lead 2','FFR Lead 3','FFR Lead 4','FFR Lead 5','FFR Lead 6',...
    'Signal 0','Signal 1','Signal 2','Signal 3','Signal 4'}; 
report.shocks.big={'Factor 1','Factor 2','ID Signal 1','ID Signal 2','ID Signal 3','ID Signal 4'}; 

report.compare.base={'FFR 1q Ahead','FFR 2q Ahead','FFR 3q Ahead','FFR 4q Ahead','FFR 5q Ahead',...
    'FFR 6q Ahead','Signal Zero','Signal One','Signal Two','Signal Three','Signal Four'}; 
report.shocks.base={'Factor 1','Factor 2','ID Signal 1','ID Signal 2','ID Signal 3','ID Signal 4'}; 


%% 1. Load Selected Part of the State Space for Big Model 
cd(loadFolder); 
big=load('workspace.mat','Y'); 
big=setfield(big,'parMode',getfield(load('workspace.mat','parMode'),'parMode')); 
big=setfield(big,'funcmod',getfield(load('workspace.mat','funcmod'),'funcmod')); 
big=setfield(big,'funcmod',getfield(load('workspace.mat','funcmod'),'funcmod')); 
big=setfield(big,'addsol',getfield(load('workspace.mat','addsol'),'addsol')); 
big=setfield(big,'solveopt',getfield(load('workspace.mat','solveopt'),'solveopt')); 
big=setfield(big,'trainvec',getfield(load('workspace.mat','trainvec'),'trainvec')); 
big=setfield(big,'sample',getfield(load('workspace.mat','sample'),'sample'));
big=setfield(big,'parnames',getfield(load('workspace.mat','parnames'),'parnames')); 
big=setfield(big,'stateNames',getfield(load('workspace.mat','stateNames'),'stateNames')); 
big=setfield(big,'shockNames',getfield(load('workspace.mat','shockNames'),'shockNames')); 
big=setfield(big,'sampleSS',getfield(load('workspace.mat','sampleSS'),'sampleSS')); 

cd(cucd); 

printcell([report.compare.big(:) report.compare.base(:)]); 
quer('c'); 

%% 2. Solve Big Model 
[GG, RR, CC, eu, SDX, ZZ, initss, ssvec,~, ssNames,stateNames,shockNamesStru]...
    =feval(big.funcmod,big.parMode,big.solveopt,big.addsol);

%% 3. Define nameStru, Structure with the Names 
shockNames=shockNamesStru.long; 
[Znames,ZposInStates]=extractObsNames(ZZ(:,:,1),stateNames);
nameStru.stateNames=stateNames;
nameStru.shockNames=shockNames;
nameStru.obsNames=Znames;
shockNames=shockNamesStru.long; 

%% 4. Kalman Filter and Define sampleStr, Structure with all the sample information
sampleStru=big.sampleSS; 
[KFBig,logL]=feval(big.addsol.funcKfilter,big.parMode,big.funcmod,big.Y,...
    big.trainvec,big.solveopt,big.addsol); 

%% 5. Load Selected Part of the State Space for Base Model 
cd(loadFolderBase); 
base=load('workspace.mat','Y'); 
base=setfield(base,'parMode',getfield(load('workspace.mat','parMode'),'parMode')); 
base=setfield(base,'funcmod',getfield(load('workspace.mat','funcmod'),'funcmod')); 
base=setfield(base,'funcmod',getfield(load('workspace.mat','funcmod'),'funcmod')); 
base=setfield(base,'addsol',getfield(load('workspace.mat','addsol'),'addsol')); 
base=setfield(base,'solveopt',getfield(load('workspace.mat','solveopt'),'solveopt')); 
base=setfield(base,'trainvec',getfield(load('workspace.mat','trainvec'),'trainvec')); 
base=setfield(base,'sample',getfield(load('workspace.mat','sample'),'sample'));
base=setfield(base,'parnames',getfield(load('workspace.mat','parnames'),'parnames')); 
base=setfield(base,'stateNames',getfield(load('workspace.mat','stateNames'),'stateNames')); 
base=setfield(base,'shockNames',getfield(load('workspace.mat','shockNames'),'shockNames')); 
cd(cucd); 


%% 6. Trim Sample of Big Model to the Base 
if isequal(big.sample,base.sample)~=1 
    warning('Samples from base and Big Model Not the same'); 
    big.posSampStart=find(big.sample == base.sample(1) );
    big.posSampEnd  =find(big.sample == base.sample(end));
    big.posSamp     =(big.posSampStart:big.posSampEnd)';

end 

%% 7. Kalman Filter Base Model 
[KFBase,logL]=feval(base.addsol.funcKfilter,base.parMode,base.funcmod,...
    base.Y,base.trainvec,base.solveopt,base.addsol); 

%% 8. Extract Positions to Compare 
% 8.a States
bigPos=cellposition(report.compare.big,big.stateNames); 
basePos=cellposition(report.compare.base,base.stateNames); 

% 8.b Shocks 
bigShockPos=cellposition(report.shocks.big,big.shockNames); 
baseShockPos=cellposition(report.shocks.base,base.shockNames); 

%% 9. Compare Filtered States 
Ncompare=length(bigPos); 
figVec=zeros(Ncompare,1); 
for ii=1:Ncompare; 
    figVec(ii)=figure; 
    plot2samp( base.sample,[KFBig.filteredSt( big.posSamp,bigPos(ii) ) ...
KFBase.filteredSt(:,basePos(ii) )],'Big','Base'); 
title(['Comparison Big and Base ',report.compare.big{ii} ] ); 
end 

compileGraphsPDF(figVec,'Filtered States',saveFolder); 
close all; clear figVec; 

%% 10. Compare Filtered Shocks 
Ncompare=length(bigShockPos);
figVec=zeros(Ncompare,1);
for ii=1:Ncompare;
    figVec(ii)=figure;
    plot2samp( base.sample,[KFBig.innovations( big.posSamp,bigShockPos(ii) ) ...
        KFBase.innovations(:,baseShockPos(ii) )],'Big','Base');
    title(['Comparison Big and Base ',report.shocks.big{ii} ] );
end
compileGraphsPDF(figVec,'Smooth Innovations',saveFolder); 
close all; clear figVec; 

%% 11. 
Ncompare=length(bigPos); 
figVec=zeros(Ncompare,1); 
for ii=1:Ncompare; 
    figVec(ii)=figure; 
    plot2samp( base.sample,[KFBig.smoothSt( big.posSamp,bigPos(ii) ) ...
KFBase.smoothSt(:,basePos(ii) )],'Big','Base'); 
title(['Comparison Big and Base ',report.compare.big{ii} ] ); 
end 
compileGraphsPDF(figVec,'Smooth States',saveFolder); 
close all; clear figVec; 

%% 12 
[KFAlt1,logL1]=feval(big.addsol.funcKfilter,parAlt(:,1),big.funcmod,big.Y,...
    big.trainvec,big.solveopt,big.addsol); 

[KFAlt2,logL2]=feval(big.addsol.funcKfilter,parAlt(:,2),big.funcmod,big.Y,...
    big.trainvec,big.solveopt,big.addsol); 

%% Comparison Alternative States 
paperPosition=[1 1 7 9];
Ncompare=length(bigPos);
figVec=zeros(Ncompare,1);
flagVline=1; 
for ii=1:Ncompare;
    figVec(ii)=figure;
    plot2samp( big.sample,[KFAlt1.filteredSt( :,bigPos(ii) ) ...
        KFAlt2.filteredSt(:,bigPos(ii) )],'Big Alt1','Big Alt2');
    titcell(1,:)={'Comparison of Filtered States in 10 Signal Model'};
    titcell(2,:)={[report.compare.big{ii},' Alt 1 (factors) vs. Alt 2 (ID)']};
    handTit=title(titcell);
    set(handTit,'FontSize',14);
    set(gca,'Box','Off');
    if flagVline==1
        handVline=vline(2007.25);
        set(handVline,'Color',[1 0 0],'LineWidth',2);
    end
    set(figVec(ii),'PaperPosition',paperPosition);
end
compileGraphsPDF(figVec,'alternative modes filtered states',saveFolderAlt); 
close all; clear figVec; 


%%
paperPosition=[1 1 7 9];
Ncompare=length(bigShockPos);
figVec=zeros(Ncompare,1);
flagVline=1; 
for ii=1:Ncompare;
    figVec(ii)=figure;
    plot2samp( big.sample,[KFAlt1.innovations( :,bigShockPos(ii) ) ...
        KFAlt2.innovations(:,bigShockPos(ii) )],'Big Alt1','Big Alt2');
    titcell=cell(2,1); 
    titcell(1,:)={'Comparison of Innovation in 10 Signal Model'}; 
    titcell(2,:)={[report.shocks.big{ii},' Alt 1 (factors) vs. Alt 2 (ID)']}; 
    handTit=title(titcell);
    set(handTit,'FontSize',14); 
    set(gca,'Box','Off');
    if flagVline==1
        handVline=vline(2007.25);
        set(handVline,'Color',[1 0 0],'LineWidth',2);
    end
    set(figVec(ii),'PaperPosition',paperPosition);
end
compileGraphsPDF(figVec,'alternative modes Innovations',saveFolderAlt);
close all; clear figVec; 

%% Comparison Alternative States 
paperPosition=[1 1 7 9];
Ncompare=length(bigPos);
figVec=zeros(Ncompare,1);
flagVline=1; 
for ii=1:Ncompare;
    figVec(ii)=figure;
    plot2samp( big.sample(end-20:end),[KFAlt1.smoothSt(end-20:end,bigPos(ii) ) ...
        KFAlt2.smoothSt(end-20:end,bigPos(ii) )],'Big Alt1','Big Alt2');
    titcell(1,:)={'Comparison of Smooth States in 10 Signal Model'};
    titcell(2,:)={[report.compare.big{ii},' Alt 1 (factors) vs. Alt 2 (ID)']};
    handTit=title(titcell);
    set(handTit,'FontSize',14);
    set(gca,'Box','Off');
    if flagVline==1
        handVline=vline(2007.25);
        set(handVline,'Color',[1 0 0],'LineWidth',2);
    end
    set(figVec(ii),'PaperPosition',paperPosition);
end
compileGraphsPDF(figVec,'alternative modes smooth states',saveFolderAlt); 
close all; clear figVec; 


%%
report2={'GPDm HP Rule','Average Inflation'};
bigPos2=cellposition(report2,big.stateNames); 
paperPosition=[1 1 7 9];
Ncompare=length(bigPos2);
figVec=zeros(Ncompare,1);
flagVline=1; 
for ii=1:Ncompare;
    figVec(ii)=figure;
    plot2samp( big.sample(end-20:end),[KFAlt1.smoothSt(end-20:end,bigPos2(ii) ) ...
        KFAlt2.smoothSt(end-20:end,bigPos2(ii) )],'Big Alt1','Big Alt2');
    titcell(1,:)={'Comparison of Smooth States in 10 Signal Model'};
    titcell(2,:)={[report2{ii},' Alt 1 (factors) vs. Alt 2 (ID)']};
    handTit=title(titcell);
    set(handTit,'FontSize',14);
    set(gca,'Box','Off');
    if flagVline==1
        handVline=vline(2007.25);
        set(handVline,'Color',[1 0 0],'LineWidth',2);
    end
    set(figVec(ii),'PaperPosition',paperPosition);
end



% NParBase=3*(addsol.NLeadsFFRObs+1)+71; 
% startSigParam=48; 
% posStdUU=(startSigParam+1:startSigParam+addsol.NLeadsFFRObs); 
% posAVec=(posStdUU(end)+1:posStdUU(end)+1+addsol.NLeadsFFRObs); 
% posBVec =(posAVec(end)+1:posAVec(end)+1+addsol.NLeadsFFRObs);
% posChangeSplitOne=[19 20 21 27 44 45 posStdUU posBVec]; 
% NChangeSplitOne=length(posChangeSplitOne); 
% 
